#!/bin/sh
#
# chkconfig: - 70 45
# description: The LogStash daemon processes, indexes, and searches logs centrally
# processname: logstashd
# config: /etc/sysconfig/logstash

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Defaults. Override in /etc/sysconfig/logstash
USER=logstash
OPTIONS=""
INDEX=true
PARSERS=1
DEBUG=false
DIR=/opt/logstash
CONFIG=$DIR/etc/logstashd.yaml
LOGFILE=/var/logstash/logstashd.log

if [ -f /etc/sysconfig/logstash ]; then 
	. /etc/sysconfig/logstash
fi

RETVAL=0
prog="logstashd"

start () {
	echo -n $"Starting $prog: "
        mkdir -p /var/run/logstash
        chown $USER /var/run/logstash

	if $DEBUG; then
		OPTIONS="$OPTIONS -d"
	fi
	if ! $INDEX; then
		OPTIONS="$OPTIONS -I"
	fi
	OPTIONS="$OPTIONS -p $PARSERS"
        OPTIONS="$OPTIONS --pidfile /var/run/logstash/logstashd.pid"
        OPTIONS="$OPTIONS --logfile $LOGFILE"
        OPTIONS="$OPTIONS $CONFIG"

	daemon --user $USER --pidfile /var/run/logstash/logstashd.pid \
	       $DIR/bin/logstashd $OPTIONS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/logstashd
}

stop () {
	echo -n $"Stopping $prog: "
	killproc -p /var/run/logstash/logstashd.pid $DIR/bin/logstashd
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
	    rm -f /var/lock/subsys/logstashd
	    rm -f /var/run/logstash/logstashd.pid
	fi
}

restart () {
        stop
        start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p /var/run/logstash/logstashd.pid logstashd
	;;
  restart|reload|force-reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/logstashd ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
	exit 1
esac

exit $?
